package com.gwnet.shop.service.impl;

import com.gwnet.common.core.utils.DateUtils;
import com.gwnet.shop.domain.UserCollectGoods;
import com.gwnet.shop.mapper.ShopGoodsMapper;
import com.gwnet.shop.mapper.UserCollectGoodsMapper;
import com.gwnet.shop.service.IUserCollectGoodsService;
import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户收藏店铺Service业务层处理
 * 
 * @author 张万鹏
 * @date 2024-08-01
 */
@Service
public class UserCollectGoodsServiceImpl implements IUserCollectGoodsService 
{
    @Resource
    private UserCollectGoodsMapper userCollectGoodsMapper;

    @Resource
    private ShopGoodsMapper goodsMapper;

    /**
     * 查询用户收藏店铺
     * @param userCollectGoods 用户收藏店铺主键
     * @return 用户收藏店铺
     */
    @Override
    public UserCollectGoods selectUserCollectGoodsByUserIdAndGoodsId(UserCollectGoods userCollectGoods) {
        return userCollectGoodsMapper.selectUserCollectGoodsByUserIdAndGoodsId(userCollectGoods);
    }

    /**
     * 查询用户收藏店铺列表
     * 
     * @param userCollectGoods 用户收藏店铺
     * @return 用户收藏店铺
     */
    @Override
    public List<UserCollectGoods> selectUserCollectGoodsList(UserCollectGoods userCollectGoods)
    {
        return userCollectGoodsMapper.selectUserCollectGoodsList(userCollectGoods);
    }

    /**
     * 新增用户收藏店铺
     * 
     * @param userCollectGoods 用户收藏店铺
     * @return 结果
     */
    @Transactional
    @Override
    public int insertUserCollectGoods(UserCollectGoods userCollectGoods)
    {
        try{
            userCollectGoods.setCreateTime(DateUtils.getNowDate());
            int rows = userCollectGoodsMapper.insertUserCollectGoods(userCollectGoods);
            goodsMapper.changeCollectNumPlusById(userCollectGoods.getGoodsId());
            return rows;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("您已收藏过此商品!");
        }
    }

    /**
     * 删除用户收藏店铺
     *
     * @param userCollectGoods 用户收藏店铺
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteUserCollectGoodsByUserIdAndGoodsId(UserCollectGoods userCollectGoods) {
        goodsMapper.changeCollectNumMinusById(userCollectGoods.getGoodsId());
        return userCollectGoodsMapper.deleteUserCollectGoodsByUserIdAndGoodsId(userCollectGoods);
    }


}
